package com.example.boot1;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//线程操作资源类
class MyResource{
    private volatile boolean FLAG = true; //默认开启，进行生产+消费
    private AtomicInteger atomicInteger = new AtomicInteger();
    BlockingQueue<String> blockingQueue = null;
    public MyResource(BlockingQueue<String> blockingQueue){
        this.blockingQueue = blockingQueue;
        //输出到底传入的那个实现类
        System.out.println(blockingQueue.getClass().getName());
        System.out.println();
    }
    public void myProd() throws Exception{
        String data = null;
        boolean retValue ;
        while (FLAG){
            data = atomicInteger.incrementAndGet()+"";
            retValue = blockingQueue.offer(data,2L, TimeUnit.SECONDS);
            if (retValue){
                System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"成功");
            }else{
                System.out.println(Thread.currentThread().getName()+"\t 插入队列"+data+"失败");
            }
            //为了看到效果 1 秒钟一个
            try{ TimeUnit.SECONDS.sleep(1);} catch(Exception e){e.printStackTrace();}
        }
        System.out.println(Thread.currentThread().getName()+"\t 大老板叫停，表示FLAG=false,生产动作结束！");
    }

    //理解叫停 对于消费着 如果在叫停前就等待消费着，那么此时在2秒得间隔期中，对于外层得while(FLAG)还是之前得true,当result==null return就是退出循环
    public void myConsumer() throws Exception{
        String result = null;
        while(FLAG){
            result = blockingQueue.poll(2L,TimeUnit.SECONDS);
            if(null == result || result.equalsIgnoreCase("")){
                FLAG = false;//据自己读代码这个可以不用写
                System.out.println(Thread.currentThread().getName()+"\t 超过两秒未取得，消费退出");
                System.out.println();
                System.out.println();
                return;
            }
            System.out.println(Thread.currentThread().getName()+"\t 消费队列"+result+"成功");
        }
    }
    public void stop() throws Exception{
        this.FLAG = false;
    }
}


public class ProdConsumer_BlockQueueDemo {
    public static void main(String[] args) throws Exception{
        MyResource myResource = new MyResource(new ArrayBlockingQueue<>(10));

        //生产线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"\t生产线程启动");
            try {
                myResource.myProd();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, "Prod").start();

        //消费线程
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName()+"\t 消费线程启动");
            System.out.println();
            System.out.println();
            try {
                myResource.myConsumer();
                System.out.println();
                System.out.println();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, "Consumer").start();

        try{ TimeUnit.SECONDS.sleep(5);} catch(Exception e){e.printStackTrace();}
        System.out.println();
        System.out.println();
        System.out.println();

        System.out.println("5秒钟时间到，大老板main线程叫停，活动结束");
        myResource.stop();

    }
}
